﻿# ===============================================
#  CPOLAR 配置清理脚本
#  功能: 清空 authtoken 与 email 的值（不加引号）
#  附加: 执行后自动打开 Windows 服务列表
#  版本: 1.2
#  作者: Wan Jun + ChatGPT
# ===============================================

[Console]::OutputEncoding = [System.Text.Encoding]::Default

# 打印 Banner
Write-Host ""
Write-Host "==============================================="
Write-Host "        CPOLAR 配置清理工具 (PowerShell)       "
Write-Host "==============================================="
Write-Host ""

# 获取当前用户配置路径
$cpolarPath = Join-Path $env:USERPROFILE ".cpolar\cpolar.yml"

if (Test-Path $cpolarPath) {
    Write-Host "[√] 找到配置文件: $cpolarPath"

    # 备份文件
    $backup = "$cpolarPath.bak"
    Copy-Item $cpolarPath $backup -Force
    Write-Host "[+] 已备份到: $backup"

    # 读取内容（ANSI/UTF8兼容）
    $content = Get-Content $cpolarPath -Raw

    # 清空 authtoken 与 email 的值（不加引号）
    $content = $content -replace '(?m)^(\s*authtoken\s*:\s*).+$', '$1'
    $content = $content -replace '(?m)^(\s*email\s*:\s*).+$', '$1'

    # 写回文件（使用默认编码，兼容 Windows）
    Set-Content -Path $cpolarPath -Value $content -Encoding Default

    Write-Host "[✓] 已清空 authtoken 和 email 的值"
} else {
    Write-Host "[×] 未找到配置文件: $cpolarPath"
}

Write-Host ""
Write-Host "-----------------------------------------------"
Write-Host "  操作完成，如需恢复，请使用备份文件 .bak"
Write-Host "-----------------------------------------------"
Write-Host ""

# 打开服务管理器
Write-Host "[→] 正在打开 Windows 服务管理器 (services.msc)..."
Start-Process "services.msc"

# 提示如何查找 Cpolar Service
Write-Host ""
Write-Host "请在打开的窗口中查找服务名称: Cpolar Service"
Write-Host "可右键 → 属性 → 查看可执行路径或执行重启操作。"
Write-Host ""
Pause
